from django.contrib import admin
from .models import Project


@admin.register(Project)
class ProjectAdmin(admin.ModelAdmin):
    """项目管理"""
    list_display = (
        'name', 'semester', 'admin_manager', 'budget', 
        'get_used_budget', 'get_remaining_budget', 'get_subproject_count', 'created_at'
    )
    list_filter = ('semester', 'admin_manager', 'created_at')
    search_fields = ('name', 'description', 'semester')
    readonly_fields = ('created_at', 'updated_at', 'get_used_budget', 'get_remaining_budget')
    
    fieldsets = (
        ('基本信息', {
            'fields': ('name', 'description', 'semester', 'admin_manager')
        }),
        ('经费信息', {
            'fields': ('budget', 'get_used_budget', 'get_remaining_budget')
        }),
        ('时间信息', {
            'fields': ('created_at', 'updated_at'),
            'classes': ('collapse',)
        }),
    )
    
    def get_used_budget(self, obj):
        """已使用经费"""
        return f"{obj.used_budget:.2f} 元"
    get_used_budget.short_description = '已使用经费'
    
    def get_remaining_budget(self, obj):
        """剩余经费"""
        return f"{obj.remaining_budget:.2f} 元"
    get_remaining_budget.short_description = '剩余经费'
    
    def get_subproject_count(self, obj):
        """子项目数量"""
        return obj.subproject_count
    get_subproject_count.short_description = '子项目数量'